package com.platform.mobile.operator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.platform.base.SysException;
import com.platform.mobile.bean.AddHospitalRecordRequest;
import com.platform.mobile.bean.ReqBean;
import com.platform.mobile.bean.ResBean;
import com.platform.mobile.bean.ResultBean;
import com.platform.mobile.factory.Operator;
import com.platform.mobile.service.PaitentService;

@Component
public class AddHospitalRecord extends Operator{

	private static Logger log = LoggerFactory.getLogger(AddHospitalRecord.class);
	
	@Autowired
	private PaitentService service;
	
	@SuppressWarnings("deprecation")
	@Override
	public ResBean operate(ReqBean reqBean) throws SysException {
		
		log.info("进入到AddHospitalRecord业务处理类，处理[" + reqBean.getClientInfo() + "]用户请求");
		ResBean resBean = new ResBean();
		resBean.setClientInfo(reqBean.getClientInfo());
		resBean.setJsonrpc("2.0");
		resBean.setMethod("AddHospitalRecord");
		ResultBean result = new ResultBean();
		
		AddHospitalRecordRequest request = new AddHospitalRecordRequest();
		
		try{
			
			Object params = reqBean.getParams();
			if(params != null){
				
				@SuppressWarnings("unchecked")
				LinkedHashMap<String, Object> paramMap = (LinkedHashMap<String, Object>)params;
				request.setPatient_id((Integer)paramMap.get("patient_id"));
				request.setDept_name(paramMap.get("dept_name").toString());
				request.setDept_id((Integer)paramMap.get("dept_id"));
				request.setMajor_name(paramMap.get("major_name").toString());
				request.setMajor_id((Integer)paramMap.get("major_id"));
				request.setAid_name(paramMap.get("aid_name").toString());
				request.setAid_id((Integer)paramMap.get("aid_id"));
				
				if(paramMap.get("note") != null){
					request.setNote(paramMap.get("note").toString());
				}

				Date admission = new Date(paramMap.get("admission_date").toString());
				request.setAdmission_date(admission);
				
				if(paramMap.get("discharge_date") != null){
					Date discharge = new Date(paramMap.get("discharge_date").toString());
					request.setDischarge_date(discharge);
				}
				
				if(paramMap.get("inhiscode") != null){
					request.setInhiscode(paramMap.get("inhiscode").toString());
				}
				
				service.addHospitalRecord(request);
				
				result.setData(null);
				result.setInfo("新增住院信息成功");
				result.setResultStatus("1");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			result.setData(null);
			result.setInfo("新增住院信息失败");
			result.setResultStatus("0");
		}
		
		resBean.setResult(result);
		return resBean;
	}

}
